package info.tsumuji.adcmp;

import jp.Adlantis.Android.AdlantisView;
import jp.co.nobot.libAdMaker.AdMakerListener;
import jp.co.nobot.libAdMaker.libAdMaker;
import android.app.Activity;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.google.ads.Ad;
import com.google.ads.AdListener;
import com.google.ads.AdRequest;
import com.google.ads.AdSize;
import com.google.ads.AdView;
import com.google.ads.AdRequest.ErrorCode;

public class AdCmpActivity extends Activity implements AdListener, AdMakerListener {

    /**
     *  AdMob
     */
    private AdView _adView = null;

    /**
     *  AdMaker
     */
    private libAdMaker _adMaker = null;

    /**
     * AdLantis‎
     */
    private AdlantisView _adLantis = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        /**
         * AdMob
         *
         * GoogleAdMobAdsSdk-4.3.1.jar
         */
        LinearLayout adMobLayout = (LinearLayout)findViewById(R.id.admob_layout);
        String ADMOB_PUBLISHER_CODE = getString(R.string.admob_publisher_code);
        _adView = new AdView(this, AdSize.BANNER, ADMOB_PUBLISHER_CODE);
        _adView.setAdListener(this);
        AdRequest request = new AdRequest();
        request.setTesting(true);
        _adView.loadAd(request);
        adMobLayout.addView(_adView);


        /**
         * AdMaker
         *
         * libAdmaker.jar
         */
        LinearLayout adMakerLayout = (LinearLayout)findViewById(R.id.admaker_layout);
        String ADMAKER_URL = getString(R.string.admaker_url);
        _adMaker = new libAdMaker(this);
        _adMaker.siteId = "111";
        _adMaker.zoneId = "1111";
        _adMaker.setUrl(ADMAKER_URL);
        adMakerLayout.addView(_adMaker);
        _adMaker.start();

        /**
         * AdLantis‎
         *
         * adlantis.jar
         */
        LinearLayout adlantisLayout = (LinearLayout)findViewById(R.id.adlantis_layout);
        String ADLANTIS_PUBLISHER_ID = getString(R.string.adlantis_publisher_id);
        _adLantis = new AdlantisView(this);
        _adLantis.setPublisherID(ADLANTIS_PUBLISHER_ID);
        adlantisLayout.addView(_adLantis);

    }


    @Override
    protected void onDestroy() {
        if (_adLantis != null) {
            // ??
        }
        if (_adMaker != null) {
            _adMaker.destroy();
            _adMaker = null;
        }
        if (_adView != null) {
            _adView.destroy();
            _adView = null;
        }
        super.onDestroy();
    }

    //--------------------------------------------------------------------------
    // [begin]implements AdListener
    //--------------------------------------------------------------------------
    @Override
    public void onDismissScreen(Ad ad) {
        Toast.makeText(this, "[AdMob]onDismissScreen", Toast.LENGTH_SHORT);
    }

    @Override
    public void onFailedToReceiveAd(Ad ad, ErrorCode errorCode) {
        Toast.makeText(this, "[AdMob]onFailedToReceiveAd", Toast.LENGTH_SHORT);
      if(ad.isReady()){
          ad.loadAd(new AdRequest());
      }
    }

    @Override
    public void onLeaveApplication(Ad ad) {
        Toast.makeText(this, "[AdMob]onLeaveApplication", Toast.LENGTH_SHORT);
    }

    @Override
    public void onPresentScreen(Ad ad) {
        Toast.makeText(this, "[AdMob]onPresentScreen", Toast.LENGTH_SHORT);
    }

    @Override
    public void onReceiveAd(Ad ad) {
        Toast.makeText(this, "[AdMob]onReceiveAd", Toast.LENGTH_SHORT);
    }
    //--------------------------------------------------------------------------
    // [end]implements AdListener
    //--------------------------------------------------------------------------
    //--------------------------------------------------------------------------
    // [begin]implements AdMakerListener
    //--------------------------------------------------------------------------
    @Override
    public void onFailedToReceiveAdMaker(String error) {
        Toast.makeText(this, "[AdMaker]onFailedToReceiveAdMaker\n" + error, Toast.LENGTH_SHORT);
    }
    @Override
    public void onReceiveAdMaker() {
        Toast.makeText(this, "[AdMaker]onReceiveAdMaker", Toast.LENGTH_SHORT);
    }
    //--------------------------------------------------------------------------
    // [end]implements AdMakerListener
    //--------------------------------------------------------------------------



}